package servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Board;
import DAO.BoardDao;

public class BoardNewAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Board board = new Board();
		BoardDao boardDao = new BoardDao();
		
		HttpSession session=request.getSession();

		ActionForward forward=new ActionForward();
		
			
		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userId = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
		if (userId < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}
		
		request.setAttribute("studyId", studyId);	
		String title = request.getParameter("title")!= null && request.getParameter("title") 
				!= ""? request.getParameter("title") : null;	
		String content = request.getParameter("content")!= null && request.getParameter("content") 
				!= ""? request.getParameter("content") : null;	
		
			

				
			
		if(content==null || title==null)  {
			//error return
			Util.ShowError(response, "값을 올바로 입력해주세요");
			
			return null;
		}

		title=Util.toUTF8(title);
		content=Util.toUTF8(content);
		board.setSid(studyId);
		board.setUid(userId);
		board.setTitle(title);
		board.setContent(content);
		
		
		boardDao.insertBoardInfo(board);
	
		
	
		// go to details studyId;
	
		forward.setPath("./study_board.jsp");
		return forward;
	}


}
